package com.stellar.circle.controller.miniApp.sComment;

import com.google.common.base.Preconditions;
import com.stellar.circle.controller.admin.sComment.vo.CommentTreeVO;
import com.stellar.circle.controller.admin.sComment.vo.SCommentRequestPageVO;
import com.stellar.circle.controller.admin.sComment.vo.SCommentRequestVO;
import com.stellar.circle.controller.admin.sComment.vo.SCommentResponseVO;
import com.stellar.circle.controller.miniApp.sComment.vo.SCommentTreeResponseVO;
import com.stellar.circle.domain.sComment.SCommentDomainService;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.bean.Result;
import com.stellar.web.core.constants.AuthUserConstants;
import com.stellar.web.core.validator.ValidatorRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 评论表控制器
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-19 14:52:26
 */
@RequestMapping("/api-miniApp/sComment")
@RestController("miniAppSCommentController")
@Slf4j
public class SCommentController {

    @Resource
    private SCommentDomainService sCommentDomainService;

    /**
     * 获取树形评论
     */
    @GetMapping("/tree")
    public Result<CommentTreeVO> getCommentTree(@RequestParam String sourceType,
                                                @RequestParam Long sourceId) {

        return Result.success(sCommentDomainService.getCommentTree(sourceType, sourceId));
    }

    /**
     * 新增评论表
     * SourceId：评论关联的来源ID，如帖子ID或闲置物品ID等
     * SourceType：评论关联的来源类型，如帖子、闲置物品等 - post-帖子/idle-闲置物品
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody SCommentRequestVO requestVO) {
        try {

            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestVO, "请求体不能为空");
                Preconditions.checkNotNull(requestVO.getSourceId(), "关联来源ID不能为空");
                Preconditions.checkNotNull(requestVO.getSourceType(), "来源类型不能为空");
                Preconditions.checkNotNull(requestVO.getContent(), "评论内容不能为空");
            }

            return Result.success(sCommentDomainService.insert(requestVO));
        } catch (IllegalArgumentException iae) {
            log.error("SCommentController.create.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }

    /**
     * 删除评论表
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(sCommentDomainService.delete(id));
    }

}
